package IT.DataAccess;

import IT.Entities.ClienteDireccion;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DatClienteDireccion implements IDatClienteDireccion{
    
    private List<ClienteDireccion> oLista;
    private ClienteDireccion oDireccion;
    private BDConexion oBase;    
    private Connection oConexion;
    private CallableStatement oCst;    
    private ResultSet oRst;    
    
    public List<ClienteDireccion> Listar() {
        
        oLista=new ArrayList<ClienteDireccion>();
         
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteDireccionListar()}");
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                oLista.add(new ClienteDireccion( oRst.getInt("cod_dir_in"),
                        oRst.getInt("tip_dir_in"), oRst.getString("Tipo"), 
                        oRst.getString("des_dir_vc"), oRst.getString("ref_dir_vc"), 
                        oRst.getString("ubi_dir_vc"), oRst.getString("Ubigeo"), 
                        oRst.getBoolean("prin_dir_bo"), oRst.getString("Principal"), 
                        oRst.getInt("est_dir_in"), oRst.getString("Estado"), 
                        oRst.getInt("cod_cli_in")));                
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
         return oLista;
    }

    public ClienteDireccion Ind(int nCodigo) {
             
        oDireccion=new ClienteDireccion();
         
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteDireccionInd(?)}");
            oCst.setInt(1, nCodigo);
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                oDireccion.setCod_dir_in( oRst.getInt("cod_dir_in"));
                oDireccion.setTip_cli_in(oRst.getInt("tip_dir_in")); 
                oDireccion.setTipo(oRst.getString("Tipo"));
                oDireccion.setDes_dir_vc( oRst.getString("des_dir_vc"));
                oDireccion.setRef_dir_vc(oRst.getString("ref_dir_vc"));
                oDireccion.setUbi_dir_vc(oRst.getString("ubi_dir_vc"));
                oDireccion.setUbigeo(oRst.getString("Ubigeo"));
                oDireccion.setPrin_dir_bo(oRst.getBoolean("prin_dir_bo"));
                oDireccion.setPrincipal(oRst.getString("Principal"));
                oDireccion.setEst_dir_in(oRst.getInt("est_dir_in"));
                oDireccion.setEstado(oRst.getString("Estado"));
                oDireccion.setCod_cli_in(oRst.getInt("cod_cli_in"));                
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
         return oDireccion;
    }

    public List<ClienteDireccion> Ubi(int nCliente) {
         oLista=new ArrayList<ClienteDireccion>();
         
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteDireccionUbi(?)}");
            oCst.setInt(1, nCliente);
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                oLista.add(new ClienteDireccion( oRst.getInt("cod_dir_in"),
                        oRst.getInt("tip_dir_in"), oRst.getString("Tipo"), 
                        oRst.getString("des_dir_vc"), oRst.getString("ref_dir_vc"), 
                        oRst.getString("ubi_dir_vc"), oRst.getString("Ubigeo"), 
                        oRst.getBoolean("prin_dir_bo"), oRst.getString("Principal"), 
                        oRst.getInt("est_dir_in"), oRst.getString("Estado"), 
                        oRst.getInt("cod_cli_in")));                
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
         return oLista;
    }

    public void Insertar(ClienteDireccion ent) {
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteDireccionInsertar(?,?,?, ?,?,?, ?)}");
          
            oCst.setInt(1, ent.getTip_dir_in());
            oCst.setString(2, ent.getDes_dir_vc());
            oCst.setString(3, ent.getRef_dir_vc());
            oCst.setString(4, ent.getUbi_dir_vc());
            oCst.setBoolean(5, ent.isPrin_dir_bo());
            oCst.setInt(6, ent.getEst_dir_in());
            oCst.setInt(7, ent.getCod_cli_in());
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Editar(ClienteDireccion ent) {        
          try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteDireccionEditar(?,?,?, ?,?,?, ?,?)}");
            
            oCst.setInt(1, ent.getCod_dir_in());
            oCst.setInt(2, ent.getTip_dir_in());
            oCst.setString(3, ent.getDes_dir_vc());
            oCst.setString(4, ent.getRef_dir_vc());
            oCst.setString(5, ent.getUbi_dir_vc());
            oCst.setBoolean(6, ent.isPrin_dir_bo());
            oCst.setInt(7, ent.getEst_dir_in());
            oCst.setInt(8, ent.getCod_cli_in());
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Eliminar(int nCodigo) {
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteDireccionEliminar(?)}");            
            oCst.setInt(1, nCodigo);
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int Valida(ClienteDireccion ent) {
         int nResultado=0;
        
         try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteDireccionValida(?,?,?)}");              
                                   
            oCst.setInt(1, ent.getCod_dir_in());
            oCst.setInt(2, ent.getCod_cli_in());            
            oCst.registerOutParameter(3, java.sql.Types.INTEGER);
            
            oCst.execute();
            nResultado=oCst.getInt(3);
            
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
        return nResultado;
    }
}
